package com.datapermission.thread;

import com.datapermission.aspect.PermissionContext;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
public class CustomThreadPoolExecutor extends ThreadPoolExecutor {

    public CustomThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);
        // 这里添加线程执行完成后的后置处理逻辑
        log.info("任务执行完成后的后置逻辑");
        if (t != null) {
            log.error("任务执行时遇到了异常", t);
        }
        // 清理threadLocal
        PermissionContext.manualClear();
        log.info("完成清理线程池的threadLocal");
    }
}
